package com.toocms.jinjuhang.ui.mine.receiver_address

import android.os.Bundle
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.StringUtils
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.config.UserRepository
import com.toocms.jinjuhang.data.center.AddressDetailsData
import com.toocms.jinjuhang.dialog.hint.HintDialog
import com.toocms.jinjuhang.dialog.save_receiver_address.SaveReceiverAddressDialog
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.jinjuhang.widget.view_model.showToast
import com.toocms.tab.base.BaseActivity
import com.toocms.tab.base.ItemViewModel
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand
import com.toocms.tab.bus.Messenger

class ReceiverAddressItemModel(
    viewModel: ReceiverAddressModel, val item: ObservableField<AddressDetailsData>
) : ItemViewModel<ReceiverAddressModel>(viewModel) {

    val itemClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        item.get()?.apply data@{
            if (Constants.Extra.ACTION_ACQUIRE != viewModel.actionObservableField.get()) return@BindingAction
            if ("0".equals(use_organ)) {
                Messenger.getDefault().send(this, Constants.MessengerToken.TOKEN_ADDRESS)
                viewModel.finishFragment()
            } else {
                SaveReceiverAddressDialog().apply {
                    this@apply.address = this@data.address
                    onPositiveClickListener = {
                        Messenger.getDefault()
                            .send(this@data, Constants.MessengerToken.TOKEN_ADDRESS)
                        viewModel.finishFragment()
                    }
                    show(
                        (ActivityUtils.getTopActivity() as BaseActivity).supportFragmentManager,
                        null
                    )
                }
            }
        }

    })

    val editClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        item.get()?.apply {
            val bundle = Bundle()
            bundle.putString(Constants.BundleKey.KEY_ADR_ID, adr_id)
            viewModel.startFragment(EditReceiverAddressFgt::class.java, bundle)
        }
    })

    //删除地址
    val deleteClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        item.get()?.apply {
            HintDialog().apply {
                content = StringUtils.getString(R.string.str_delete_receiver_address_hint)
                onPositiveClickListener = { tag ->
                    val params = mutableMapOf<String, String?>()
                    params["member_id"] = UserRepository.user.member_id
                    params["adr_id"] = adr_id
                    requestData(Urls.Center.DEL_ADDRESS, params, resultClass = String::class.java,
                        result = {
                            showToast(it)
                            viewModel.items -= this@ReceiverAddressItemModel
                        })
                }
                show((ActivityUtils.getTopActivity() as BaseActivity).supportFragmentManager, null)
            }
        }
    })


}